package top.vanchor.myblog2back.controller.user;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import top.vanchor.myblog2back.entity.BlogFeedback;
import top.vanchor.myblog2back.entity.BlogReport;
import top.vanchor.myblog2back.service.IBlogFeedbackService;
import top.vanchor.myblog2back.service.IBlogReportService;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-03-26
 */
@Api("反馈控制器")
@RestController
@RequestMapping("/blogFeedback")
public class BlogFeedbackController {
    @Resource
    private IBlogFeedbackService service;

    @ApiOperation("用户提交反馈")
    @PostMapping("/toAdd")
    @ResponseBody
    public JSONObject toReport(@RequestBody BlogFeedback bean){
        JSONObject jsonObject = new JSONObject();
        try {
            bean.setCreateTime(String.valueOf(System.currentTimeMillis()/1000));
            bean.setFeedbackStatus(0);
            bean.setUserId(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo()))));
            if (service.toReport(bean) > 0){
                jsonObject.put("code",200);
                jsonObject.put("msg","反馈成功");
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","反馈失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常");
        }
        return jsonObject;
    }
}
